//
//  AddAppointmentVC.m
//  Toyota
//
//  Created by ld on 2024/6/27.
//

#import "AddAppointmentVC.h"
#import "BottomSelectionBoxView.h"
#import "BRPickerView.h"
#import "PopUpTipsView.h"
#import <LSTPopView.h>
#import "Definition.h"
#import "Helper.h"
@interface AddAppointmentVC ()<UITextFieldDelegate> {
    UITextField * timeTF;
    UITextField * beginTimeTF;
    UITextField * endTF;
    NSArray * selectTimeArr;
    NSDate * minDate;
}
@property (nonatomic, strong) UIButton * openBtn;

@end

@implementation AddAppointmentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColorFFFFFF;
    
    [self iniitUI];
    // Do any additional setup after loading the view.
}

-(void)iniitUI {
    NSArray * cellArr = @[@"日期", @"开始锁定时间", @"结束锁定时间"];
    NSArray * valArr = @[];
    if (_editModel) {
        valArr = @[_editModel.week, _editModel.beginTime, _editModel.endTime];
        selectTimeArr = [_editModel.week componentsSeparatedByString:@"，"];
    }
    for (int i = 0; i < cellArr.count; i++) {
        UIControl * cellCon = [[UIControl alloc]initWithFrame:CGRectMake(0, 50 * i, ScreenWidth, 50)];
        cellCon.tag = 1000 + i;
        [cellCon addTarget:self action:@selector(cellConAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:cellCon];
        
        UILabel * keyLab = [[UILabel alloc]initWithFrame:CGRectMake(25, 0, 200, cellCon.customHeight)];
        keyLab.text = cellArr[i];
        keyLab.textColor = kColor141414;
        keyLab.font = fontSize(16);
        [cellCon addSubview:keyLab];
        
        UITextField * valTF = [[UITextField alloc]initWithFrame:CGRectMake(keyLab.customRight, 0, cellCon.customWidth - keyLab.customRight - 25, cellCon.customHeight)];
        valTF.textColor = kColor141414;
        valTF.font = fontSize(15);
        valTF.delegate = self;
        valTF.placeholder = @"请选择";
        valTF.textAlignment = NSTextAlignmentRight;
        valTF.enabled = NO;
        [cellCon addSubview:valTF];
        if (_editModel) {
            valTF.text = valArr[i];
        }
        if (i == 0) {
            timeTF = valTF;
        }else if (i == 1) {
            beginTimeTF = valTF;
        }else if (i == 2) {
            endTF = valTF;
        }
        
        UILabel * lineLab = [[UILabel alloc]initWithFrame:CGRectMake(25, cellCon.customHeight - 1, cellCon.customWidth - 50, 1)];
        lineLab.backgroundColor = kColorEDEDED;
        [cellCon addSubview:lineLab];
        
    }
    
    _openBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.view addSubview:_openBtn];
    [_openBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-CYScalH(5) - CYSafeAreaBottom);
        make.size.mas_equalTo(CGSizeMake(CYScalW(344), CYScalW(40)));
    }];
    [_openBtn setTitle:@"保存" forState:UIControlStateNormal];
    [_openBtn setBackgroundImage:[UIImage imageWithColor:kColorC2C2C2] forState:(UIControlStateNormal)];
    if (_editModel) {
        [_openBtn setBackgroundImage:[UIImage imageWithColor:kColor141414] forState:(UIControlStateNormal)];
    }else {
        _openBtn.enabled = NO;
    }
    _openBtn.titleLabel.font = fontSize(15);
    _openBtn.clipsToBounds = YES;
    _openBtn.layer.cornerRadius = CYScalW(20);
    [_openBtn addTarget:self action:@selector(openBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    [self addObserversToTextField:timeTF];
    [self addObserversToTextField:beginTimeTF];
    [self addObserversToTextField:endTF];
}

-(void)cellConAction:(UIControl *)sender {
    if (sender.tag == 1000) {
//        选择时间
        [self choiceWeekTime];
    }else if (sender.tag == 1001) {
        [self choiceTimeBoxWithTitle:@"开始锁定时间" andTF:beginTimeTF];
    }else if (sender.tag == 1002) {
        [self choiceTimeBoxWithTitle:@"结束锁定时间" andTF:endTF];
    }
}

-(void)choiceWeekTime {
    [LSTPopView removeAllPopView];
    BottomSelectionBoxView * view = [[BottomSelectionBoxView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, CYScalXH(420)) andTitleStr:@"选择日期" andMsgStr:@"" andCellArray:@[@"周一", @"周二", @"周三", @"周四", @"周五", @"周六", @"周日"] andIsModel:NO andChoiceArr:selectTimeArr hasMulti:YES];
    view.customHeight = view.bgView.customHeight;
    view.backgroundColor = [UIColor whiteColor];
    [view round:10 RectCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)];
    LSTPopView *popView = [LSTPopView initWithCustomView:view popStyle:LSTPopStyleFade dismissStyle:LSTDismissStyleFade];
    __block LSTPopView *blockFooter = popView;
    popView.popDuration = 0.5;
    popView.hemStyle = LSTHemStyleBottom;
    popView.dismissDuration = 0.5;
    popView.bgAlpha = 0.5;
    [popView pop];
    view.closeBtnClick = ^{
        [blockFooter dismiss];
    };
    view.choiceMultiBtnClick = ^(NSArray * _Nonnull btnArr) {
        self->selectTimeArr = btnArr;
        self->timeTF.text  = [btnArr componentsJoinedByString:@"，"];
        [blockFooter dismiss];
    };
   
}

-(void)choiceTimeBoxWithTitle:(NSString *)title andTF:(UITextField *)tf {
    // 1.创建日期选择器
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
    // 2.设置属性
    datePickerView.pickerMode = BRDatePickerModeHM;
    datePickerView.title = title;
    if (tf == endTF) {
        datePickerView.minDate = minDate;
    }
    datePickerView.isAutoSelect = YES;
    datePickerView.resultActionBlock = ^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
        tf.text = selectValue;
        if (tf == self->beginTimeTF) {
            self->minDate = selectDate;
        }
    };
    
    // 设置自定义样式
    BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
    customStyle.pickerColor = [UIColor whiteColor];
    customStyle.titleTextColor = kColor141414;
    customStyle.hiddenTitleLine = YES;
    customStyle.titleTextFont = fontBold(16);
    customStyle.pickerTextColor = kColor141414;
    customStyle.separatorColor = kColor969696;
    customStyle.separatorColor = kColorEDEDED;
    datePickerView.pickerStyle = customStyle;

    // 3.显示
    [datePickerView show];
}

-(void)openBtnClick:(UIButton *)sender {
    if (self.addModelBlock) {
        AppointmentAntiTheftModel * model = [AppointmentAntiTheftModel new];
        model.week = timeTF.text;
        model.beginTime = beginTimeTF.text;
        model.endTime = endTF.text;
        self.addModelBlock(model);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)addObserversToTextField:(UITextField *)textField {
    [textField addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
}
 
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"text"]) {
        [self openBtnChoiceStatus:[timeTF.text length] > 0 && [beginTimeTF.text length] && [endTF.text length] > 0];
    }
    
}

-(void)openBtnChoiceStatus:(BOOL)enabled {
    [_openBtn setEnabled:enabled];
    if (enabled) {
        [_openBtn setBackgroundImage:[UIImage imageWithColor:kColor141414] forState:(UIControlStateNormal)];
    }else {
        [_openBtn setBackgroundImage:[UIImage imageWithColor:kColorC2C2C2] forState:(UIControlStateNormal)];
        
    }
}
 
- (void)dealloc {
    [timeTF removeObserver:self forKeyPath:@"text"];
    [beginTimeTF removeObserver:self forKeyPath:@"text"];
    [endTF removeObserver:self forKeyPath:@"text"];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
